//
//  HZComposeTextView.swift
//  HansWeibo
//
//  Created by Hans on 16/8/17.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit

class HZComposeTextView: UITextView {
    // 重写UITextView font 属性
    override var font: UIFont?{
        didSet{
            // 设置占位文字的font
            placeholderLabel.font = font
            
        }
    }
    // 显示占位文字
    var placeholder: String?{
        didSet{
            //设置占位文字的text
            placeholderLabel.text = placeholder
        }
    }
    
    

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 设置视图
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
//        let text = UITextField()
        // 添加控件
        addSubview(placeholderLabel)
        //添加约束
        placeholderLabel.snp_makeConstraints { (make) in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(SCREENW - 10)
        }
        // 监听系统通知   查看textView 文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFieldTextDidChange", name: UITextFieldTextDidChangeNotification, object: nil)
        
    }
    // 监听文字改变
    func textFieldTextDidChange(){
        
        placeholderLabel.hidden = self.hasText()
        
    }
    
    // 析构函数 销毁通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // MARK: - 懒加载控件
    private lazy var placeholderLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.grayColor()
//        label.font = UIFont.systemFontOfSize(12)
//        label.text = "萌萌哒汉子"
        label.numberOfLines = 0
        return label
    }()
    
}
